package cn.plugins.generator.generate

import cn.plugins.generator.common.FreemarkerConstant
import cn.plugins.generator.entity.TableEntity
import cn.plugins.generator.utils.PrintInfoUtil
import freemarker.template.Configuration
import freemarker.template.Template
import java.io.*
import java.nio.charset.StandardCharsets

/**
 *
 * @描述：生成业务逻辑层
 * @时间：2021/3/1 5:26 下午
 * @作者：jy
 * @公司：贵州中测信息技术有限公司
 */
object GenerateService {

    /**
     * 生成service-freemarker模板
     */
    fun freemarkerGenerate(
        frameworkType: String,
        config: Configuration,
        projectPath: String,
        packageName: String,
        dic: String,
        languageType: String,
        tableInfo: TableEntity,
        dataMap: MutableMap<String, Any?>
    ) {
        // mybatis版本
        if (frameworkType == "mybatis") {
            val fileName =
                "${tableInfo.className}Service.${if (languageType == "java") FreemarkerConstant.JAVA_FILE_SUFFIX else FreemarkerConstant.KOTLIN_FILE_SUFFIX}"
            val fileFullPath =
                "${projectPath}/src/main/${languageType}/${packageName.replace(".", "/")}/${dic}/${fileName}"

            val file = File(fileFullPath)
            if (file.exists()) {
                println("message: file [${fileName}] is exists.")
                return
            }
            if (!file.exists()) {
                file.parentFile.mkdirs()

                PrintInfoUtil.printCreateFold(file.parentFile.absolutePath)
            }

            val template: Template = if (languageType == "java") {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/java/$frameworkType/${FreemarkerConstant.SERVICE_TEMPLATE_JAVA}",
                    "UTF-8"
                )
            } else {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/kotlin/$frameworkType/${FreemarkerConstant.SERVICE_TEMPLATE_KOTLIN}",
                    "UTF-8"
                )
            }

            val out: Writer = BufferedWriter(OutputStreamWriter(FileOutputStream(fileFullPath), StandardCharsets.UTF_8))
            template.process(dataMap, out)
            out.flush()
            out.close()

            PrintInfoUtil.printCreateFile(fileFullPath)
        } else if (frameworkType == "mybatisplus") {// mybatisplus版本
            // Iservice
            val iFileName =
                "I${tableInfo.className}Service.${if (languageType == "java") FreemarkerConstant.JAVA_FILE_SUFFIX else FreemarkerConstant.KOTLIN_FILE_SUFFIX}"
            val iFileFullPath =
                "${projectPath}/src/main/${languageType}/${packageName.replace(".", "/")}/${dic}/${iFileName}"

            val iFile = File(iFileFullPath)
            if (iFile.exists()) {
                println("message: file [${iFileName}] is exists.")
                return
            }
            if (!iFile.exists()) {
                iFile.parentFile.mkdirs()

                PrintInfoUtil.printCreateFold(iFile.parentFile.absolutePath)
            }

            var template: Template = if (languageType == "java") {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/java/$frameworkType/${FreemarkerConstant.ISERVICE_TEMPLATE_JAVA}",
                    "UTF-8"
                )
            } else {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/kotlin/$frameworkType/${FreemarkerConstant.ISERVICE_TEMPLATE_KOTLIN}",
                    "UTF-8"
                )
            }

            val iOut: Writer = BufferedWriter(OutputStreamWriter(FileOutputStream(iFileFullPath), StandardCharsets.UTF_8))
            template.process(dataMap, iOut)
            iOut.flush()
            iOut.close()

            PrintInfoUtil.printCreateFile(iFileFullPath)

            // service
            val fileName =
                "${tableInfo.className}ServiceImpl.${if (languageType == "java") FreemarkerConstant.JAVA_FILE_SUFFIX else FreemarkerConstant.KOTLIN_FILE_SUFFIX}"
            val fileFullPath =
                "${projectPath}/src/main/${languageType}/${packageName.replace(".", "/")}/${dic}/impl/${fileName}"

            val file = File(fileFullPath)
            if (file.exists()) {
                println("message: file [${fileName}] is exists.")
                return
            }
            if (!file.exists()) {
                file.parentFile.mkdirs()

                PrintInfoUtil.printCreateFold(file.parentFile.absolutePath)
            }

            template = if (languageType == "java") {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/java/$frameworkType/${FreemarkerConstant.SERVICE_TEMPLATE_JAVA}",
                    "UTF-8"
                )
            } else {
                config.getTemplate(
                    "${FreemarkerConstant.BASIC_DIC}/kotlin/$frameworkType/${FreemarkerConstant.SERVICE_TEMPLATE_KOTLIN}",
                    "UTF-8"
                )
            }

            val out: Writer = BufferedWriter(OutputStreamWriter(FileOutputStream(fileFullPath), StandardCharsets.UTF_8))
            template.process(dataMap, out)
            out.flush()
            out.close()

            PrintInfoUtil.printCreateFile(fileFullPath)
        }
    }
}